<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class WayDB extends BaseDB
{
    /**
     * 特效
     */

    public $tWay = 'ways';

    public function __construct()
    {
    }

    //列表
    public function getIndex($page,$size)
    {
        $query = DB::table($this->tWay)->where('del',0);
        $total = $query->count();
        $models = $query->skip($page)->take($size)->orderBy('id','asc')->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $items[] = [
                'id'=>$model['id'],
                'cover'=>$model['cover'], 'dn'=>env('DOMAIN').'/',
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    //一个
    public function getOne($id)
    {
        $models = DB::table($this->tWay)->where('id',$id)->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $model = $this->jsonToArr($models[0]);
        $files = $model['demand'] ? json_decode($model['demand'], true) : '';
        $items = [
            'id'=>$model['id'], 'dn'=>env('DOMAIN').'/', 'files'=>$files,
            'cover'=>$model['cover'], 'link'=>$model['link'],

        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    //获取一个
    public function getOldOne()
    {
        $resOnes = DB::table($this->tWay)->where('del',0)->where('cover','')
            ->skip(0)->take(1)->orderBy('id','asc')->get();
        if (!count($resOnes)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $resOne = $this->jsonToArr($resOnes[0]);
        return array('code'=>'0','msg'=>'success','data'=>$resOne);
    }

    //获取需求素材
    public function getDemand($id)
    {
        $resOnes = DB::table($this->tWay)->where('id',$id)
            ->skip(0)->take(1)->orderBy('id','asc')->get();
        if (!count($resOnes)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $resOne = $this->jsonToArr($resOnes[0]);
//        $demands = json_decode($resOne['demand'], true);
        $items = array(); $dn = env('DOMAIN').'/';
        if ($file=$resOne['demand']) {
            $jsons = json_decode($file, true); $items = $jsons;
            foreach ($jsons as $k => $json) {
//                if ($json['cover']) {
//                    $items[$k]['cover'] = $json['cover'] ? $dn.$json['cover'] : '';
//                }
//                if ($json['link']) {
//                    $items[$k]['link'] = $json['link'] ? $dn.$json['link'] : '';
//                }
                $items[$k]['dn'] = $dn;
            }
        }
        return array('code'=>'0','msg'=>'success','data'=>$resOne,'files'=>$items);
    }

    //更新GIF
    public function setCover($id,$cover)
    {
        //开启事务
        DB::beginTransaction();
        try {
            DB::table($this->tWay)->where('id',$id)->update(['cover'=>$cover]);
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }
}